package com.carkeeper.system.controller;

import com.carkeeper.common.utils.Result;
import com.carkeeper.common.utils.StatusCode;
import com.carkeeper.order.feign.OrderFeign;
import com.carkeeper.order.pojo.Order;
import com.carkeeper.system.service.AdminService;
import com.carkeeper.system.service.UserService;
import com.carkeeper.system.util.CookieUtil;
import com.carkeeper.system.util.JwtUtil;
import org.bouncycastle.jcajce.provider.symmetric.SEED;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.constraints.Null;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * @author pengjinxiong
 * @version 1.0
 * @date 2020/11/26 9:33
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Value("${auth.cookieDomain}")
    private String cookieDomain;
    @Value("${auth.cookieMaxAge}")
    private int cookieMaxAge;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private OrderFeign orderFeign;
    @Autowired
    private UserService userService;

    /**
     * 登录
     *
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/login")
    public Result login(String username, String password, HttpServletResponse response) {
        Map<String, Object> map = adminService.login(username, password);
        if (map != null) {
            String uid = (String) map.get("uid");
            CookieUtil.addCookie(response, cookieDomain, "/", "uid", uid, cookieMaxAge, false);
            return new Result(true, StatusCode.OK, "登录成功", map);
        } else {
            return new Result(false, StatusCode.LOGINERROR, "用户名或密码错误");
        }

    }

    // 退出
    @RequestMapping("/exit")
    public Result exit(HttpServletRequest request) {
        //获取cookie中的uid
        Cookie[] cookies = request.getCookies();
        String uid = cookies[0].getValue();
        redisTemplate.delete(uid);
        return new Result(true, StatusCode.OK, "退出成功");
    }

    // 修改密码
    @PutMapping
    public Result update(@RequestBody Map<String, String> map) {
        String username = map.get("username");
        String oldPassword = map.get("oldPassword");
        String newPassword = map.get("newPassword");
        Result result = userService.changePassword(username, oldPassword, newPassword);
        return result;
    }

    //流水折线图数据
    @GetMapping("/getMemberReport")
    public Result getMemberReport(@RequestParam Map searchMap) throws ParseException {
        Date searchDate = (Date) searchMap.get("searchDate");

        Map<String, Object> map = new HashMap<>();

        if (searchDate == null) {
            List<String> list = new ArrayList();
            Calendar calendar = Calendar.getInstance();//获得日历对象，模拟时间就是当前时间
            //计算过去一月的30天
            calendar.add(Calendar.DAY_OF_MONTH, -30);//获得当前时间往前推30天的时间
            Date date1 = calendar.getTime();
            for (int i = 0; i < 30; i++) {
                calendar.add(Calendar.DAY_OF_MONTH, 1);//获得当前时间往后推一个月日期
                Date date = calendar.getTime();
                list.add(new SimpleDateFormat("MM-dd").format(date));
            }
            map.put("list", list);
            String format = new SimpleDateFormat("yyyy-MM-dd").format(date1);
            searchMap.put("date",format);
        }
        List<Map> moneyByDate = orderFeign.findMoneyByDate(searchMap);
        map.put("moneyList", moneyByDate);

        return new Result(true, StatusCode.OK, "", map);


    }


}
